package com.wyzz.hr.bo.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IEcptypeBO;
import com.wyzz.hr.dao.IEcptypeDAO;
import com.wyzz.hr.pojo.Ecptype;

public class EcptypeBOImpl implements IEcptypeBO {

	public EcptypeBOImpl() {
		dao = null;
	}

	public List FindAllEcptype() {
		DetachedCriteria dc = DetachedCriteria.forClass(Ecptype.class);
		dc.addOrder(Order.asc("ecptypeSortId"));
		return dao.findByCriteria(dc);
	}

	public boolean addEcptype(Ecptype ecptype) {
		try {
			String trimmedEcptypeName = ecptype.getEcptypeName().trim();
			ecptype.setEcptypeName(trimmedEcptypeName);
			ecptype.setEcptypeSortId(Integer.valueOf(0));
			dao.saveObject(ecptype);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean delEcptype(Class name, String id) {
		try {
			List check = new ArrayList();
			boolean isRefed = false;
			check = dao.exeHqlList((new StringBuilder()).append(
					"select count(*) from Empsalaryadj where esaEcptypeId = '")
					.append(id).append("'").toString());
			if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
				isRefed = true;
			Ecptype oldEcptype = (Ecptype) dao.loadObject(name, id, null,
					new boolean[0]);
			if (!isRefed && oldEcptype != null) {
				dao.deleteObject(oldEcptype);
				return true;
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	public boolean updateEpcategory(Ecptype ecptype) {
		Ecptype oldEcptype = (Ecptype) dao.loadObject(Ecptype.class, ecptype
				.getId(), null, new boolean[0]);
		if (oldEcptype != null) {
			String trimmedEcptypeName = ecptype.getEcptypeName().trim();
			oldEcptype.setEcptypeName(trimmedEcptypeName);
			oldEcptype.setEcptypeDesc(ecptype.getEcptypeDesc());
			dao.updateObject(oldEcptype);
			return true;
		} else {
			return false;
		}
	}

	public void saveEcpTypeByBatch(String ids[]) {
		if (ids == null)
			return;
		int sort = 1;
		int len = ids.length;
		for (int i = 0; i < len; i++) {
			dao.exeHql((new StringBuilder()).append(
					"update Ecptype set ecptypeSortId=").append(sort).append(
					" where id ='").append(ids[i]).append("'").toString());
			sort++;
		}

	}

	public List getEcptypeList(Ecptype ecptype) {
		DetachedCriteria dc = DetachedCriteria.forClass(Ecptype.class);
		dc.add(Restrictions.eq("ecptypeName", ecptype.getEcptypeName().trim()));
		dc.add(Restrictions.ne("id", ecptype.getId()));
		return dao.findByCriteria(dc);
	}

	public List getEcptypeByName(String name) {
		DetachedCriteria dc = DetachedCriteria.forClass(Ecptype.class);
		dc.add(Restrictions.eq("ecptypeName", name.trim()));
		return dao.findByCriteria(dc);
	}

	public IEcptypeDAO getDao() {
		return dao;
	}

	public void setDao(IEcptypeDAO dao) {
		this.dao = dao;
	}

	private IEcptypeDAO dao;
}
